#include<iostream>
#include<vector>

using namespace std;

/**/

class NeighborSum {
private:
    vector<int> adx = { 0, 0, 1, -1 };
    vector<int> ady = { 1, -1, 0, 0 };

    vector<int> dix = { -1, -1, 1, 1 };
    vector<int> diy = { 1, -1, 1, -1 };

    int _adjacentSum(int _x, int _y) {
        int sum = 0;
        for (int i = 0; i < 4; i++) {
            int x = _x + adx[i], y = _y + ady[i];
            if (x >= 0 && x < row && y >= 0 && y < col)
                sum += _grid[x][y];
        }
        return sum;
    }

    int _diagonalSum(int _x, int _y) {
        int sum = 0;
        for (int i = 0; i < 4; i++) {
            int x = _x + dix[i], y = _y + diy[i];
            if (x >= 0 && x < row && y >= 0 && y < col)
                sum += _grid[x][y];
        }
        return sum;
    }
public:
    NeighborSum(vector<vector<int>>& grid)
        : _grid(grid), row(grid.size()), col(grid[0].size()) {}

    int adjacentSum(int value) {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (_grid[i][j] == value)
                    return _adjacentSum(i, j);
            }
        }
        return -1;
    }

    int diagonalSum(int value) {
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (_grid[i][j] == value)
                    return _diagonalSum(i, j);
            }
        }
        return -1;
    }

private:
    vector<vector<int>> _grid;
    int row;
    int col;
};

int main()
{
    vector<vector<int>> grid = { {1,2,3},{4,5,6},{7,8,9} };
    NeighborSum ns(grid);
    cout << ns.adjacentSum(1) << endl;
    cout << ns.diagonalSum(4) << endl;
    cout << ns.adjacentSum(4) << endl;
    cout << ns.diagonalSum(8) << endl;
    return 0;
}